This
page is under construction.
Ports?!?
IP addresses? TCP/IP or protocols?!?!?
UDP is like the US mail... Is it? :-) Euh, I just may have to tell you whtt it's all about. A most simplistic view: every device on the Internet has a number, an IP address. Every device also has a number of ports. A program on your computer can use those ports to talk to other computers... Another way to look at it:
PureBasic offers you some build-in commands, and if those do not suffy you can use WinAPI to call WinSock. 1. A really simple network with two PC's would probably look something like this: PC1 --- SWITCH --- PC22. Typically we simply ignore switches and hubs (they won't do anything smart to our data) and thus we could look at the network like this: PC1 --- PC2I do like the simple things in life :-) 3. Let's say you have a single PC and a modem (and huge phonebills :-)) that connects you to the Internet: PC1 --- MODEM --- INTERNET4. If you have an ADSL or cable modem things look pretty much the same. PC1 --- ADSLMODEM --- INTERNET5. Most ADSL modems are no longer simple modems, but are smart devices, messing around with the data we pass through them. In other words... they are routers and modems in one! And most of them are smart enough to protect you somewhat from the dangers of the Internet. Just a little, mind you... To confuse matters, ADSL routers are often called ADSL modems. Anyway... PC1 --- ROUTER --- INTERNET6. Some routers have only a single network port, so if you run a network you may have to add a switch... PC1 --- SWITCH --- ROUTER --- INTERNET7. Obviously, multiple machines could be connected to that switch... PC1 ----8. Switches are fairly stupid, so there's no reason why you can't stack them. Traffic will find its way... PC2 --- SWITCH --- SWITCH --- SWITCH --- SWITCH --- ROUTER --- INTERNET9. WiFi boxes could be considered another kind of switch (with a little added protection). Think of a WiFi link as just another piece of cable... PC2 --- WIFI --- SWITCH --- SWITCH --- SWITCH --- ROUTER --- INTERNET10. Just make sure you don't create loops... they just might kill your network! -------11. Many modern day routers may have their switch and router 'build in', so you can connect your PC's to them directly, without the use for a seperate box. Though logically it would still be seperate blocks, physically it's all in a single box, which we tend to call a router (though it's actually a router + WiFi + switch + firewall + modem all mixed up): PC1 ----12. A LAN is a local network. It's a bunch of PC's hooked up together through a switch. It's what you find in companies, or in a geek's home ;-) Between the different PC's there's little security, and local network speeds are typically high. Great for games, sharing music, watching movies, or for more serious things like accessing documents on a central office server. Let's repeat example number 7 once more... PC1 ----13. The LAN (Local Area Network) is on the left side... PC1 ----14. And the WAN (Wide Area Network) is on the right side... --- ROUTER --- INTERNET(I hope the web and your browser won't mess up these little drawings...) With these basics under your belt go on the Internet and try to understand NAT. It's an essential part (and source of many problems) when it comes to IP network related programming. If you don't have multiple machines at hand, you may opt for using a virtual machine like VirtualBox. (Which is why I did spend so much time on VM's for more pure PureBasic happines...) In fact, it's often a lot more practial to have it all on one system in front of you. Only when dealing with CPU intensive applications (games), or applications that need exclusive access to your hardware (more games) you may need multiple dedicated machines to bring enough horsepower to your desktop. Some tools / applications you definitely want to take a look at before diving in...
Install all machines, and if you know you can trust your router / firewall you may disable any software firewalls on your PC, just for testing. If you're concerned about your safety, disconnect from the Internet before you do so and reconnect afterwards. Software firewalls can seriously cause confusion when you start with network programming. For the next steps, I used the following setup:
14.3 Build-in commands This section is under construction. This section is under construction. This section is under construction.
FTP using the Windows client Not exactly a PureBasic topic in itself, yet it may help you verifying the workings of your own code. The example below is what I use to update CodeCaddy, and is similar to what I use to create the survival guide archive and then upload it onto my homepage. The first part is a batch file that creates a folder, then copies a selected set of files into it, and then packs it into a zip file. Yeah, I know. No PureBasic at all in this part, except for a folder name :-) rem *** this batch script creates an archive containing codecaddyFirst I create a few variables for the batch file, so I can change paths in a single place. Other people may prefer to hardcode all paths and do a search / replace, it's just a matter of taste. My distribution of CodeCaddy is a subset of all files in the CodeCaddy related folders, so I create a dedicated folder and fill it with the files and folders I want to distribute. (That's all those 'copy' lines.) I then use (the free) 7zip to pack the whole shebang into a zip archive (which I place in my local offline copy of my website). I use the 7zip compatibility mode to make sure older / other (un)packers can handle the final zip file. I've now created a zip file containing the latest public version of CodeCaddy, but I might also have changed the related page 'purebasic 29.htm' on the website and I need to upload them both. Windows XP comes with its own FTP client, called... wait for it... tada... ftp.exe.You can choose to either use it online / interactive, or to use a script. To find out all about ftp.exe and its command line parameters type: ftp.exe -?I use two ftp.exe options: -s to load and execute a script and -n suppress a password prompt: ftp.exe -n -s:<scriptfile>Have a look at the following script. I've stored it on my 'v-drive' which actually is a TrueCrypt encrypted drive. (If someone would nick my PC then he / she would not have my passwords and private documents). open ftp.xs4all.nl 21From top to bottom:
mput v:\data\webpages\nightlive\purebasic\*.jpgWith a bit of creativity and some googling you should now be able to write your own ftp scripts for the Windows ftp client, and you might execute them from within PureBasic using the RunProgram() command. But where's the fun in that... :-)
Look for the basic topics here. This section is under construction. PC's on the Internet find each other by using 'public' Internet addresses. Typically a firewall or router seperates the big bad Internet from your home or office network. To the outside world it looks like all PC's in your home network have a single address. Don't worry how the router figures out where to send information to if you requested something from another machine on the Internet. Better question yourself what the router will do with UNREQUESTED information / packets... This is where port forwarding comes in. Assume you and someone else are running a game or other application over the Internet. Let's say both sides have a router+PC, like this: PC1 --- ROUTER1 --- ISP1 --- INTERNET --- ISP2 --- ROUTER2 --- PC2The IP addresses may be assigned dynamically, or be fixed. (Following addresses are chosen at random and don't mean much.) I assume you both have a fixed IP. Many people have their machine assigned an address by DHCP on the router, ie. the router defines the address of the PC on the local network. PC1 (DHCP client) --- ROUTER1 (DHCP server assigning address to client PC)This, typically, may not be such a good idea. You want to be sure the PC is always on the same address in your local network, so you can forward incoming traffic to it. So disable DHCP on your client and assign it a fixed IP address, OR configure your router in such a way that it will always assign a specific IP address to your PC (this may not always be possible). PC1 192.168.0.21 --- 192.168.0.99 ROUTER1Now the router itself MAY be assigned a fixed address by your ISP, or it may be assigned a dynamical address by your ISP. If it's a fixed address then life will be easier. If you AND the other party both have a dynamic address you're pretty much out of luck, unless you find a third party so you and the other party can exchance their current adresses. Or you type them in by hand every time, that will work as well :-) Depending on your configuration and setup, you may have to either specify the router address as your DNS server, or use the DNS server of your ISP. I tend to do the latter, as routers don't always play nice when it comes to DNS. The gateway address is typically the address of the router itself. I assume the ISP assigns permanent IP address to your router, resulting in something like: PC1 192.168.0.21 --- 192.168.0.99 ROUTER1 172.31.1.14 --- ISP1 --- ...(Yes, that router has two addresses, one local to your network, and one to the outside world.) Traffic incoming from the internet has to be redirected to some PC on your network. If that traffic was a reply to something your PC send out (such as a typical HTTP session) it will be routed to that PC. But if it is incoming and NOT a reply to something you send out, it will be stuck at the router. The router doesn't know where to send it to. So, what you have to do is a port forward, ie. someone on the internet tries to make a connection to a port on your router, and your router redirects it to your PC. How to set up such a forwarded port depends on your router. The ports don't need to match. You may set up a rule like this: Router 172.31.1.14 port 6000 should be forwarded to 192.168.0.21 port 7000.Now everytime something on the big bad Internet tries to contact your PC, it does so by sending packets to 172.31.1.14 port 6000, and the PC receives them on port 7000. Obviously, the same applies to the other side as well. Hooking up two machines could look something like this: PC1 192.168.0.21:7000 --- 192.168.0.99 ROUTER 172.31.1.14:6000 --- ISP1 --- INTERNET --- ISP2 --- 131.32.4.17:7000 ROUTER 10.0.0.151 --- 10.0.0.1:5555 PC2Duh. Sounds difficult, but actually it isn't.
Now if BOTH sides have dynamically assigned public addresses, things are a little more complicated. You will need a third party that is contacted by both clients to exchange proper IP adresses. Obviously that third party needs a fixed address otherwise both clients cannot find it... There's a way around, and that is using dynamic DNS adresses. But if you understand dynamic DNS, then you won't need my advice :-) I just wanted to mention it... For many games / applications both parties have to open up certain ports and add port forwarding rules. There are also those that do not need such rules, either because they always route through a central location, OR because they punch holes in the firewall in Skype style. (Google a little and you will see what I mean.) Can of worms alert! Do a Google, and find out why many people disable this functionality on their routers. Things have improved, but still... This section is under construction. |